<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 登录日志
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
        <div class="handle-box">
                <div class="block">
                登录时间:
                <el-date-picker
                    v-model="value1"
                    type="datetimerange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                    value-format="yyyy-MM-dd HH:mm:ss">
                </el-date-picker>
                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-download" @click="export2Excel">导出为Excel</el-button>
            </div>
            </div>
            <el-table
            :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center" type="index"></el-table-column>
                <el-table-column prop="account" label="用户名"></el-table-column>
                <el-table-column prop="realname" label="真实姓名"></el-table-column>
                <el-table-column prop="logintime" label="登录时间" :formatter="formatDate"></el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
    </div>
</template>

<script>
import { loginmsg } from '../../api/index';
import { searchLoginmsg } from '../../api/index';
import { exportLoginmsg } from '../../api/index';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                pageIndex: 1,
                pageSize: 10,
                value1: ''
            },
            value1:[],
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1,
            time:{
                pageIndex: 1,
                pageSize: 10
            },
            listData:[],
        };
    },
    created() {
        this.getData();
    },
    methods: {
        formatDate(row, column) {
            // 获取单元格数据
            let data = row[column.property]
            if(data == null) {
                return null
            }
            let dt = new Date(data)
                return dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate() + ' ' + dt.getHours() + ':' + dt.getMinutes() + ':' + dt.getSeconds()
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            loginmsg(this.query).then(res => {
                console.log(res);
                this.tableData = res.t.list;
                this.pageTotal = res.t.total;
            });
            exportLoginmsg(this.query).then(res => {
                console.log(res);
                this.listData = res.t;
            });
        },
        // 触发搜索按钮
        handleSearch() {
            if(this.value1!==null && this.value1!==''){
                this.time.start = this.value1[0];
                this.time.end = this.value1[1];
            } else {
                this.time.start = null;
                this.time.end = null
            }
            if(this.time.start == null && this.time.end == null){
                loginmsg(this.query).then(res => {
                    console.log(res);
                    this.tableData = res.t.list;
                    this.pageTotal = res.t.total;
                 });
            }else{
            searchLoginmsg(this.time).then(res => {
                console.log(res);
                this.tableData = res.t.list;
                this.pageTotal = res.t.total;
            });
            console.log(this.value1);
            this.$set(this.query, 'pageIndex', 1);
            // this.getData();
            }
        },
    
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },

        //导出Excel
        export2Excel() {
        　require.ensure([], () => {
        　　　const { export_json_to_excel } = require('../../excel/Export2Excel');
        　　　const tHeader = ['账号', '真实姓名', '登录时间'];
        　　　// 上面设置Excel的表格第一行的标题
              const filterVal = ['account', 'realname', 'logintime'];
        　　　const list = this.listData;  //把data里的tableData存到list
        　　　const data = this.formatJson(filterVal, list);
            //下载数据
        　　　export_json_to_excel(tHeader, data, '意见反馈列表');
        　　})
        　　},
        　　formatJson(filterVal, jsonData) {
            return jsonData.map(v => filterVal.map(j => v[j]))
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
